import 'package:flutter/material.dart';

import 'base/UrlPath.dart';
import 'js/ts_webview_js_page.dart';
import 'shared_normalview_1static/ts_view_addto_diffpage.dart'
    as diffpage_1static;
import 'shared_normalview_2param/ts_view_addto_diffpage.dart'
    as diffpage_2param;
import 'shared_normalview_3pd/ts_view_addto_diffpage.dart' as diffpage_3pd;
import 'shared_webview/ts_web_addto_diffpage.dart' as diffwebpage_static;
import 'shared_webview_pd/ts_web_addto_diffpage_pd.dart' as diffwebpage_pd;
import 'test_web_controller_util.dart';

class MyHomePage extends StatefulWidget {
  final String title;
  const MyHomePage({super.key, required this.title});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Stack(
          children: [
            ListView(
              children: [
                _renderItem(
                  '❓测试一个视图实例添加到不同viewController上(1static)',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) {
                        return diffpage_1static.TSViewAddToDiffPage();
                      },
                    ));
                  },
                ),
                _renderItem(
                  '❓测试一个视图实例添加到不同viewController上(2param)',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) {
                        return diffpage_2param.TSViewAddToDiffPage();
                      },
                    ));
                  },
                ),
                _renderItem(
                  '❓测试一个视图实例添加到不同viewController上(3pd)',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) {
                        return diffpage_3pd.TSViewAddToDiffPage();
                        // return MultiProvider(
                        //   providers: [
                        //     ChangeNotifierProvider(
                        //         create: (_) =>
                        //             TSSharedNormalViewContainerModel()),
                        //   ],
                        //   child: diffpage_3pd.TSViewAddToDiffPage(),
                        // );
                      },
                    ));
                  },
                ),
                Container(height: 20),
                _renderItem(
                  '❓测试 shared webView (static 方式)',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) {
                        String url = UrlPath.gameUrl;
                        return diffwebpage_static.TSWebAddToDiffPage(
                          url: url,
                        );
                      },
                    ));
                  },
                ),
                _renderItem(
                  '❓测试 shared webView (pd 方式)',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) {
                        return diffwebpage_pd.TSWebAddToDiffPage();
                      },
                    ));
                  },
                ),
                Container(height: 20),
                _renderItem(
                  '测试js',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    Navigator.push(context, MaterialPageRoute(
                      builder: (context) {
                        return const TSWebViewJSPage();
                      },
                    ));
                  },
                ),
                Container(height: 20),
                _renderItem(
                  '测试webViewWidget',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    ShareWebViewController.goTSWebViewJSPage(context,
                        urlParams: null, methodTpye: 0);
                  },
                ),
                _renderItem(
                  '测试webcontroller',
                  alignment: Alignment.centerLeft,
                  onTap: () async {
                    ShareWebViewController.goTSWebViewJSPage(context,
                        urlParams: null, methodTpye: 1);
                  },
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  _rennderItemPage({
    required String title,
    AlignmentGeometry alignment = Alignment.center,
    required Widget page,
  }) {
    return _renderItem(
      title,
      alignment: alignment,
      onTap: () {
        // JSUtil.deleteAllCacheData();
        Navigator.push(context, MaterialPageRoute(
          builder: (context) {
            return page;
          },
        ));
      },
    );
  }

  _renderItem(
    String title, {
    required Function onTap,
    AlignmentGeometry alignment = Alignment.center,
  }) {
    return InkWell(
      onTap: () {
        onTap();
      },
      child: Column(
        children: [
          Container(
            height: 44,
            alignment: alignment,
            child: Text(title),
          ),
          Container(
            color: Colors.black12,
            height: 0.5,
          )
        ],
      ),
    );
  }
}
